﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace linkouqs.Gamekit
{
    public class ItemUI : MonoBehaviour
    {
        #region Data
        public Item Item { get; set; }
        public int Amount { get; set; }
        #endregion

        #region UI Component
        private Image itemImage;
        private Text amountText;

        private Image ItemImage
        {
            get
            {
                if(itemImage==null)
                {
                    itemImage = GetComponent<Image>();
                }
                return itemImage;
            }
        }

        private Text AmountText
        {
            get
            {
                if(amountText==null)
                {
                    amountText = GetComponentInChildren<Text>();
                }
                return amountText;
            }
        }

        #endregion


        public void SetItem(Item item, int amount=1)
        {
            transform.localScale = Vector3.one * 1.4f;
            this.Item = item;
            this.Amount = amount;

            ItemImage.sprite = Resources.Load<Sprite>(item.Sprite);
           
            if(Item.Capacity>1)
            {
                AmountText.text = Amount.ToString();
            }
            else
            {
                AmountText.text = "";
            }


        }

        public void SetItemUI(ItemUI item)
        {
            InventoryManager.Instance.PickedItem = item;
        }

        public void AddAmount(int amount=1)
        {
            transform.localScale = Vector3.one * 1.4f;
            this.Amount += amount;

            if(Item.Capacity>1)
            {
                AmountText.text = Amount.ToString();
            }
            else
            {
                AmountText.text = "";
            }
       }

        public void ReduceAmount(int amount=1)
        {
            transform.localScale = Vector3.one * 1.4f;
            this.Amount -= amount;

            if(this.Amount<=0)
            {
                DestroyImmediate(this.gameObject);
            }

           else if(Item.Capacity>1)
            {
                AmountText.text = Amount.ToString();
            }
            else
            {
                AmountText.text = "";
            }
        }

        public void SetAmount(int amount)
        {
            transform.localScale = Vector3.one * 1.4f;
            this.Amount= amount;

            if(Item.Capacity>1)
            {
                AmountText.text = Amount.ToString();
            }
            else
            {
                AmountText.text = "";
            }
        }

        public void Show()
        {
            gameObject.SetActive(true);
        }

        public void Hide()
        {
            gameObject.SetActive(false);
        }

        public void SetLocalPosition(Vector3 Position)
        {
            transform.localPosition = Position;
        }

        public void Exchange(ItemUI itemUI)
        {
            Item itemTemp = itemUI.Item;
            int amountTemp = itemUI.Amount;
            itemUI.SetItem(this.Item, this.Amount);
            this.SetItem(itemTemp, amountTemp);
                
        }
        
    }
}
